package com.sso.managedBean;

import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.operamasks.faces.annotation.Accessible;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.DataModel;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.annotation.SaveState;
import org.operamasks.faces.component.grid.impl.UIDataGrid;
import org.operamasks.faces.user.util.Browser;

import com.base.commons.SpringBeanHolder;
import com.sso.entity.DicSystem;
import com.sso.service.IDicSystemService;
@ManagedBean(name = "sso_listSystemBean", scope = ManagedBeanScope.REQUEST)
public class ListSystemBean {
	private final static Log logger = LogFactory
	.getLog(ListSystemBean.class);
	private IDicSystemService dicSystemService = SpringBeanHolder.
			getBean("dicSystemService");
	@Inject
	private FacesContext context;
	
	@Accessible
	@SaveState
	private DicSystem system;
	
	@Bind(id = "systemNameForSearch", attribute = "value")
	@SaveState
	private String systemName_value;
	
	@Bind(id = "listSystemGrid", attribute = "binding")
	private UIDataGrid systemGrid_binding;
	
	@BeforeRender
	public void beforeRender(boolean isPostback) {
		if (!isPostback) {
			systemName_value = null;
		}
	}
	@Action(id = "search")
	public void search() {
		systemGrid_binding.reload();
	}
	
	@Action(id = "reset")
	public void reset() {
		systemName_value = null;
		systemGrid_binding.reload();
	}
	
	@DataModel(id = "listSystemGrid")
	private List<DicSystem> getSystemGrid() {
		// 搜索条件写入搜索
		List<DicSystem> chList = new ArrayList<DicSystem>();
		if (systemName_value != null && !"".equals(systemName_value)) {
			chList = dicSystemService.getBySystem(systemName_value);
		} else {
			chList = dicSystemService.getAllSystem();
		}
		return chList;
	}
	
	@Action(id = "deleteSystem")
	public void deleteSystem() {
		Object[] obj = systemGrid_binding.getSelectedValues();
		if (obj == null || obj.length == 0) {
			Browser.execClientScript("window.alert('请选择一条记录 ！');");
			return;
		}
		DicSystem sch = (DicSystem) obj[0];
		//逻辑删除
		dicSystemService.deleteDicSystem(sch);
		systemGrid_binding.reload();
	}
	
	@Action(id = "eidtSystem")
	public void eidtSystem() {
		Object[] obj = systemGrid_binding.getSelectedValues();
		if (obj == null || obj.length == 0) {
			Browser.execClientScript("window.alert('请选择一条记录 ！');");
			return;
		}
		DicSystem sch = (DicSystem) obj[0];
		String url = context.getExternalContext().getRequestContextPath()
				+ "/modules/sso/editSystem.faces?id=" + sch.getId();
		String script = "addCenterLayoutTab('eidtSystemTab','编辑系统名称','" + url
				+ "')";
		Browser.execClientScript("window.top", script);
	}
}
